import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Scanner;


public class LogAnalysis {
	Job[] jobs;
	public class cmpStart implements Comparator<Job>{
		public int compare(Job arg0, Job arg1) {
			return arg0.sTime.compareTo(arg1.sTime);
		}
	}
	public class cmpLaunch implements Comparator<Job>{
		public int compare(Job arg0, Job arg1) {
			return arg0.lTime.compareTo(arg1.lTime);
		}
	}
	public class cmpFinish implements Comparator<Job>{
		public int compare(Job arg0, Job arg1) {
			return arg0.fTime.compareTo(arg1.fTime);
		}
	}
	public class Job{
		String id;
		long submitTime;
		long launchTime;
		long finishTime;
		Date sTime, lTime, fTime;
		public Job(String i, long submit, long launch, long finish){
			id = i;
			submitTime = submit;
			launchTime = launch;
			finishTime = finish;
		}
		public Job (String i){
			id = i;
		}
		public void setSubmit(long submit){
			submitTime = submit;
			sTime = new Date(submit) ;
			
		}
		public void setLaunch(long launch){
			launchTime = launch;
			lTime = new Date(launch);
		}
		public void setFinish(long finish){
			finishTime = finish;
			fTime = new Date(finish);
		}
		public String toString(){
//			return id+"\t"+sTime.toString().substring(8)+"\t"+lTime.toString().
//			substring(8)+"\t"+fTime.toString().substring(8)+"\t"+(launchTime-submitTime)+"\t"+(finishTime-launchTime);
			return id+"\t"+sTime+"\t"+lTime+"\t"+fTime+"\t"+(launchTime-submitTime)+"\t"+(finishTime-launchTime);

		}
	}
	
	public void analysis(String fileName) throws IOException{
		jobs = new Job[20];
		Scanner sc = new Scanner(new FileInputStream(fileName));
		String line;
		int counter = 0;
		Job currentJob=null;
		while(sc.hasNextLine()){
			line = sc.nextLine();
			String[] sp = line.split("[:=,]");
			if(sp[0].trim().equals("jobID") || sp[0].trim().equals("jobId")){
				currentJob = new Job(sp[1].substring(17));
				jobs[counter] = currentJob;
				counter++;
			}
			if(sp[0].trim().equals("finishTime")){
				currentJob.setFinish(Long.parseLong(sp[1]));
			}
			if(sp[0].trim().equals("submitTime")){
				currentJob.setSubmit(Long.parseLong(sp[1]));
			}
			if(sp[0].trim().equals("launchTime")){
				currentJob.setLaunch(Long.parseLong(sp[1]));
			}
		}
		FileWriter fw = new FileWriter(fileName+"_Parsed");
		fw.write("The last two field is wait time, working time. \n");
		for(int i=0; i<counter; i++){
			Job tmp = jobs[i];
			fw.write((tmp.toString()+"\n"));
			System.out.println(tmp.toString()+"\n");
		}
		Arrays.sort(jobs,0,counter,new cmpStart());
		fw.write("Submit sequence:\n");
		for(int i=0; i<counter; i++){
			Job tmp = jobs[i];
			fw.write((tmp.id+"\t"));
		}
		fw.write("\n");
		fw.write("Launch sequence:\n");
		Arrays.sort(jobs,0,counter,new cmpLaunch());
		for(int i=0; i<counter; i++){
			Job tmp = jobs[i];
			fw.write((tmp.id+"\t"));
		}
		fw.write("\n");
		fw.write("Finish sequence:\n");
		Arrays.sort(jobs,0,counter,new cmpFinish());
		for(int i=0; i<counter; i++){
			Job tmp = jobs[i];
			fw.write((tmp.id+"\t"));
		}
		fw.write("\n");
		fw.close();
	}
	public static void main(String[] argc) throws IOException{
		LogAnalysis o = new LogAnalysis();
		o.analysis("original_log");
		o.analysis("simulation_log");
	}
}
